﻿using YooAssetsSystem.Fsm;
using UnityEngine;
using YooAsset;

namespace YooAssetsSystem.Patch
{
    /// <summary>
    /// 更新清单文件 3
    /// </summary>
    public class UpdateManifestNode : BasePatchNode
    {
        public override void OnEnter()
        {
            Owner.Event.ChangeStateEvent?.Invoke($"更新资源清单");
            var package = YooAssets.GetPackage(shareData.package);
            var operation = package.UpdatePackageManifestAsync(shareData.version);
            
            operation.Completed += delegate(AsyncOperationBase operationBase)
            {
                if (operation.Status == EOperationStatus.Succeed)
                {
                    machine.ChangeState<CreateDownloadNode>();
                }
                else
                {
                    Debug.LogError(operation.Error);
                    Owner.Event.UpdateFailedEvent?.Invoke($"{GetType().Name}失败, 停止更新");
                }
            };
        }

        public override void OnExit()
        {
            base.OnExit();
        }
    }
}